#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
from utils.log_util import get_logger


def api_request(method, url, headers, params=None):
    """
    :param method: http访问方法
    :param api: 访问api
    :param params: 额外参数，一般用于高级查询中的条件输入
    :rtype: dict
    :return: 返回查询结果
    """
    logger = get_logger()
    try:
        r = requests.request(method=method, url=url, headers=headers, json=params)
        if r.status_code == 200:
            logger.info(r.json())
            js = r.json()
            if js.get("status") == 0:
                return js
            else:
                logger.error('Error: %s' % js)
                return None
        else:
            logger.error('Error: %s, %s' % (url, r.text))
            return None
    except Exception as e:
        logger.error(e)
        return None